var express = require('express');
var router = express.Router();
let { listModel, SManagementModel, advertisingModel, platformModel, searchModel, seckillModel, groupModel, discountsModel, specialModel } = require('../../model/model')

//用户列表
router.get('/list', async function (req, res, next) {
    
    let list = await listModel.find()
    res.send({
        code: 200,
        list
    })
})
//专题列表
router.get('/sman', async function (req, res, next) {
    let list = await SManagementModel.find()
    res.send({
        code: 200,
        list
    })
})
//添加专题
router.post('/addsman', async function (req, res, next) {
    let data = req.body
    console.log(data);
    await SManagementModel.create(data)
    res.send({
        code: 200,
        msg: '添加成功'
    })
})
//回显专题
router.get('/hui', async function (req, res, next) {
    let { _id } = req.query
    let list = await SManagementModel.find({ _id: _id })
    res.send({
        code: 200,
        msg: '修改成功',
        list
    })
})
//修改专题
router.post('/editsman', async function (req, res, next) {
    let obj = req.body
    let _id = obj._id
    console.log(obj, _id);

    await SManagementModel.updateOne({ _id }, obj)
    res.send({
        code: 200,
        msg: '修改成功'
    })
})
//修改禁用启用
router.post('/editqijin', async function (req, res, next) {
    let { _id } = req.body
    const list = await SManagementModel.find({ _id })
    list[0].flag = !list[0].flag
    list[0].save()
    res.send({
        code: 200,
        msg: '修改成功'
    })
})
//删除专题
router.post('/delsman', async function (req, res, next) {
    let { _id } = req.body
    await SManagementModel.deleteOne({ _id: _id })
    res.send({
        code: 200,
        msg: '删除成功'
    })
})
//广告列表
router.get('/adver', async function (req, res, next) {
    let list = await advertisingModel.find()
    res.send({
        code: 200,
        list
    })
})
//添加广告
router.post('/addadver', async function (req, res, next) {
    let data = req.body
    console.log(data);
    await advertisingModel.create(data)
    res.send({
        code: 200,
        msg: '添加成功'
    })
})
//回显广告
router.get('/huiadver', async function (req, res, next) {
    let { _id } = req.query
    let list = await advertisingModel.find({ _id: _id })
    res.send({
        code: 200,
        msg: '修改成功',
        list
    })
})
//修改广告
router.post('/editadver', async function (req, res, next) {
    let obj = req.body
    let _id = obj._id
    console.log(obj,_id);
    
    await advertisingModel.updateOne({_id},obj)
    res.send({
        code: 200,
        msg: '修改成功'
    })
})
//广告修改上线下线
router.post('/shangxia', async function (req, res, next) {
    let { _id } = req.body
    const list = await advertisingModel.find({ _id })
    list[0].flag = !list[0].flag
    list[0].save()
    res.send({
        code: 200,
        msg: '修改成功'
    })
})
//删除广告
router.post('/deladver', async function (req, res, next) {
    let { _id } = req.body
    await advertisingModel.deleteOne({ _id: _id })
    res.send({
        code: 200,
        msg: '删除成功'
    })
})
//热词列表
router.get('/search', async function (req, res, next) {
    let list = await searchModel.find()
    res.send({
        code: 200,
        list
    })
})
//添加热词
router.post('/addsearch', async function (req, res, next) {
    let data = req.body
    console.log(data);
    await searchModel.create(data)
    res.send({
        code: 200,
        msg: '添加成功'
    })
})
//回显热词
router.get('/huisearch', async function (req, res, next) {
    let { _id } = req.query
    let list = await searchModel.find({ _id: _id })
    res.send({
        code: 200,
        msg: '修改成功',
        list
    })
})
//修改热词
router.post('/editsearch', async function (req, res, next) {
    let obj = req.body
    let _id = obj._id
    console.log(obj,_id);
    
    await searchModel.updateOne({_id},obj)
    res.send({
        code: 200,
        msg: '修改成功'
    })
})
//热词修改上线下线
router.post('/sshangxia', async function (req, res, next) {
    let { _id } = req.body
    const list = await searchModel.find({ _id })
    list[0].flag = !list[0].flag
    list[0].save()
    res.send({
        code: 200,
        msg: '修改成功'
    })
})
//删除热词
router.post('/delsearch', async function (req, res, next) {
    let { _id } = req.body
    await searchModel.deleteOne({ _id: _id })
    res.send({
        code: 200,
        msg: '删除成功'
    })
})

//平台列表
router.get('/platform', async function (req, res, next) {
    let list = await platformModel.find()
    res.send({
        code: 200,
        list
    })
})
//平台修改上线下线
router.post('/pshangxia', async function (req, res, next) {
    let { _id } = req.body
    console.log(_id);
    
    const list = await platformModel.find({ _id })
    list[0].flag = !list[0].flag
    list[0].save()
    res.send({
        code: 200,
        msg: '修改成功'
    })
})

//秒杀列表
router.get('/seckill', async function (req, res, next) {
    let list = await seckillModel.find()
    res.send({
        code: 200,
        list
    })
})
//添加秒杀
router.post('/addseckill', async function (req, res, next) {
    let data = req.body
    console.log(data);
    await seckillModel.create(data)
    res.send({
        code: 200,
        msg: '添加成功'
    })
})
// //回显秒杀
// router.get('/huisearch', async function (req, res, next) {
//     let { _id } = req.query
//     let list = await searchModel.find({ _id: _id })
//     res.send({
//         code: 200,
//         msg: '修改成功',
//         list
//     })
// })
//修改秒杀
router.post('/editseckill', async function (req, res, next) {
    let obj = req.body
    let _id = obj._id
    console.log(obj,_id);
    
    await seckillModel.updateOne({_id},obj)
    res.send({
        code: 200,
        msg: '修改成功'
    })
})
//秒杀修改上线下线
router.post('/shangxia2', async function (req, res, next) {
    let { _id } = req.body
    const list = await seckillModel.find({ _id })
    list[0].flag = !list[0].flag
    list[0].save()
    res.send({
        code: 200,
        msg: '修改成功'
    })
})
//删除秒杀
router.post('/delseckill', async function (req, res, next) {
    let { _id } = req.body
    await seckillModel.deleteOne({ _id: _id })
    res.send({
        code: 200,
        msg: '删除成功'
    })
})
//拼团列表
router.get('/group', async function (req, res, next) {
    let list = await groupModel.find()
    res.send({
        code: 200,
        list
    })
})
//添加拼团
router.post('/addgroup', async function (req, res, next) {
    let data = req.body
    console.log(data);
    await groupModel.create(data)
    res.send({
        code: 200,
        msg: '添加成功'
    })
})
// //回显秒杀
// router.get('/huisearch', async function (req, res, next) {
//     let { _id } = req.query
//     let list = await searchModel.find({ _id: _id })
//     res.send({
//         code: 200,
//         msg: '修改成功',
//         list
//     })
// })
//修改拼团
router.post('/editgroup', async function (req, res, next) {
    let obj = req.body
    let _id = obj._id
    console.log(obj,_id);
    
    await groupModel.updateOne({_id},obj)
    res.send({
        code: 200,
        msg: '修改成功'
    })
})
//拼团修改上线下线
router.post('/shangxia3', async function (req, res, next) {
    let { _id } = req.body
    const list = await groupModel.find({ _id })
    list[0].flag = !list[0].flag
    list[0].save()
    res.send({
        code: 200,
        msg: '修改成功'
    })
})
//删除拼团
router.post('/delgroup', async function (req, res, next) {
    let { _id } = req.body
    await groupModel.deleteOne({ _id: _id })
    res.send({
        code: 200,
        msg: '删除成功'
    })
})
//优惠券列表
router.get('/discounts', async function (req, res, next) {
    let list = await discountsModel.find()
    res.send({
        code: 200,
        list
    })
})
//添加优惠券
router.post('/adddiscounts', async function (req, res, next) {
    let data = req.body
    console.log(data);
    await discountsModel.create(data)
    res.send({
        code: 200,
        msg: '添加成功'
    })
})
// //回显秒杀
// router.get('/huisearch', async function (req, res, next) {
//     let { _id } = req.query
//     let list = await searchModel.find({ _id: _id })
//     res.send({
//         code: 200,
//         msg: '修改成功',
//         list
//     })
// })
//修改优惠券
router.post('/editdiscounts', async function (req, res, next) {
    let obj = req.body
    let _id = obj._id
    console.log(obj,_id);
    
    await discountsModel.updateOne({_id},obj)
    res.send({
        code: 200,
        msg: '修改成功'
    })
})
//优惠券修改上线下线
router.post('/shangxia4', async function (req, res, next) {
    let { _id } = req.body
    const list = await discountsModel.find({ _id })
    list[0].flag = !list[0].flag
    list[0].save()
    res.send({
        code: 200,
        msg: '修改成功'
    })
})
//删除优惠券
router.post('/deldiscounts', async function (req, res, next) {
    let { _id } = req.body
    await discountsModel.deleteOne({ _id: _id })
    res.send({
        code: 200,
        msg: '删除成功'
    })
})
//满减列表
router.get('/special', async function (req, res, next) {
    let list = await specialModel.find()
    res.send({
        code: 200,
        list
    })
})
//添加满减
router.post('/addspecial', async function (req, res, next) {
    let data = req.body
    console.log(data);
    await specialModel.create(data)
    res.send({
        code: 200,
        msg: '添加成功'
    })
})
// //回显秒杀
// router.get('/huisearch', async function (req, res, next) {
//     let { _id } = req.query
//     let list = await searchModel.find({ _id: _id })
//     res.send({
//         code: 200,
//         msg: '修改成功',
//         list
//     })
// })
//修改满减
router.post('/editspecial', async function (req, res, next) {
    let obj = req.body
    let _id = obj._id
    console.log(obj,_id);
    
    await specialModel.updateOne({_id},obj)
    res.send({
        code: 200,
        msg: '修改成功'
    })
})
//满减修改上线下线
router.post('/shangxia5', async function (req, res, next) {
    let { _id } = req.body
    const list = await specialModel.find({ _id })
    list[0].flag = !list[0].flag
    list[0].save()
    res.send({
        code: 200,
        msg: '修改成功'
    })
})
//删除满减
router.post('/delspecial', async function (req, res, next) {
    let { _id } = req.body
    await specialModel.deleteOne({ _id: _id })
    res.send({
        code: 200,
        msg: '删除成功'
    })
})

module.exports = router;

